<template>
  <div class="markdown-content" v-html="renderedContent"></div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { marked } from 'marked'
import DOMPurify from 'dompurify'
const props = defineProps<{
  content: string
}>()

const renderedContent = computed(() => {
  // 使用 marked 解析 Markdown
  const rawHtml = marked.parse(props.content)
  // 使用 DOMPurify 清理 HTML 以防止 XSS 攻击
  return typeof rawHtml === 'string' ? DOMPurify.sanitize(rawHtml) : ''
})
</script>

<style lang="scss" scoped>
.markdown-content {
  font-size: 14px;
  line-height: 1.6;
  color: #1C1C1E;

  :deep(h1) {
    font-size: 1.5em;
    margin: 1em 0 0.5em;
    font-weight: bold;
  }

  :deep(h2) {
    font-size: 1.3em;
    margin: 1em 0 0.5em;
    font-weight: bold;
  }

  :deep(h3) {
    font-size: 1.2em;
    margin: 1em 0 0.5em;
    font-weight: bold;
  }

  :deep(p) {
    margin: 0.5em 0;
  }

  :deep(code) {
    background-color: #f5f5f5;
    padding: 0.2em 0.4em;
    border-radius: 3px;
    font-family: monospace;
  }

  :deep(pre) {
    background-color: #f5f5f5;
    padding: 1em;
    border-radius: 4px;
    overflow-x: auto;

    code {
      background-color: transparent;
      padding: 0;
    }
  }

  :deep(blockquote) {
    border-left: 4px solid #ddd;
    margin: 0.5em 0;
    padding-left: 1em;
    color: #666;
  }

  :deep(ul), :deep(ol) {
    margin: 0.5em 0;
    padding-left: 2em;
  }

  :deep(li) {
    margin: 0.3em 0;
  }

  :deep(a) {
    color: #4E89FD;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }

  :deep(img) {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
  }

  :deep(table) {
    border-collapse: collapse;
    width: 100%;
    margin: 0.5em 0;

    th, td {
      border: 1px solid #ddd;
      padding: 0.5em;
      text-align: left;
    }

    th {
      background-color: #f5f5f5;
    }
  }
}
</style> 